//
//  InputTableViewCell.m
//  GoldBook
//
//  Created by wmz on 2021/8/18.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "InputTableViewCell.h"
@interface InputTableViewCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLB;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnWidthLAYOUT;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightLayout;
@property (weak, nonatomic) IBOutlet BaseButton *rightBtn;

@end
@implementation InputTableViewCell

- (void)hd_setupViews{
    @HDWeakify(self)
    self.textTV.add_event(^(UIView * _Nonnull sender) {
        @HDStrongify(self)
        self.model.title = self.textTV.text;
        self.textTV.add_text(self.model.title);
    });
    self.textTV.delegate = self;
    self.textTV.clearButtonMode = UITextFieldViewModeWhileEditing;
}

- (void)setGNModel:(BaseCellModel*)data{
    self.model = data;
    self.nameLB.text = GNFillEmpty(data.imageTitle);
    self.nameLB.textColor = data.detailColor;
    self.nameLB.font = data.detailFont;
    self.textTV.placeholder = data.detail;
    self.textTV.add_text(data.title);
    self.textTV.font = data.titleFont;
    self.textTV.textColor = data.titleColor;
    self.textTV.keyboardType = data.keyBoardType;
    if (GNStringNotEmpty(data.imageTitle)){
        self.widthLayout.constant = data.titleWidth?:80;
        self.leftLayout.constant = 10;
    }else{
        self.widthLayout.constant = 0;
        self.leftLayout.constant = 0;
    }
    self.textTV.showLine = data.imageHide;
    self.lineView.hidden = !data.lineHidden;
    if (data.url) {
        self.btnWidthLAYOUT.constant = 18;
        self.rightLayout.constant = 5;
        self.rightBtn.userInteractionEnabled = YES;
    }else{
        self.btnWidthLAYOUT.constant = 0;
        self.rightLayout.constant = 0;
        self.rightBtn.userInteractionEnabled = NO;
    }
    self.rightBtn.add_title(@"").add_image([UIImage imageNamed: data.url && data.url.length ? data.url : @"arrow"]);
    self.textTV.maxNum = data.index;
//    if (GNStringNotEmpty(data.imageTitle)) {
//        self.textTV.textAlignment = NSTextAlignmentRight;
//    }else{
//        self.textTV.textAlignment = NSTextAlignmentLeft;
//    }
//    self.textTV.clearsOnBeginEditing = YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (!self.model.enable) {
        [GNEvent eventResponder:self target:textField key:@"textFieldClick" indexPath:self.model.indexPath info:@{@"data":self.model}];
        return NO;
    }
    return YES;
}

- (BOOL)textField:(BaseTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *mainText = @"";
    if ([string isEqual:@""]) {
        if (![textField.text isEqualToString:@""]) {
            mainText = [textField.text substringToIndex:[textField.text length] - 1];
        }else{
            mainText = textField.text;
        }
    }else{
        mainText = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }
    
    if (textField.maxNum&&mainText.length>textField.maxNum) {
        return NO;
    }
    if (textField.inputBlock) {
        textField.inputBlock(NO, mainText);
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    
    return YES;
}

@end
